package me.sluijsens.StartersQuiz;

import java.util.logging.Logger;

import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerPickupItemEvent;
import org.bukkit.event.player.PlayerTeleportEvent;

/**
 * Handles player actions
 * @author Bryan Slop (Sluijsens)
 *
 */
public class PlayerHandler implements Listener {
	private StartersQuiz plugin;
	private QuizHandler quizHandler;
	private LangHandler lang;
	
	Logger log = Logger.getLogger("Minecraft");
	
	//Load configuration files
	private ConfigHandler conf;
	private String tagChat;
	
	public PlayerHandler(StartersQuiz instance) {
		this.plugin = instance;
	}
	
	/**
	 * Triggered when player chats, block chat when player is still doing quiz and it is configured to be blocked or give the answer if configured to do on chat
	 * @param event Event that has occurred
	 */
	@SuppressWarnings("static-access")
	@EventHandler(priority = EventPriority.HIGH)
	public void onPlayerChat(AsyncPlayerChatEvent event) {
		Player player = event.getPlayer();
		
		tagChat = plugin.tagChat;
		conf = plugin.getConf();
		quizHandler = plugin.getQuizHandler();
		lang = plugin.getLangHandler();
		
		boolean config = conf.getBoolean("Block.Chatting", true);
		boolean CommandOnChat = conf.getBoolean("CommandOnChat", false);
		
		if(plugin.checkBlock(player) && config) {
			event.setCancelled(true);
			if(CommandOnChat) {
				quizHandler.answer(event.getMessage(), player);
			} else {
				player.sendMessage(tagChat + lang.getMessage("NotifyQuizFirst"));
			}
		}
	}
	
	/**
	 * Triggered when player interacts, block when player is still doing quiz and it is configured to be blocked
	 * @param event Event that has occurred
	 */
	@SuppressWarnings("static-access")
	@EventHandler(priority = EventPriority.HIGH)
	public void onPlayerInteract(PlayerInteractEvent event) {
		Player player = event.getPlayer();
		
		tagChat = plugin.tagChat;
		conf = plugin.getConf();
		lang = plugin.getLangHandler();
		
		boolean config = conf.getBoolean("Block.Interaction", true);
		
		if(plugin.checkBlock(player) && config) {
			event.setCancelled(true);
			player.sendMessage(tagChat + lang.getMessage("NotifyQuizFirst"));
		}
	}
	
	/**
	 * Triggered when player picks up an item, block when player is still doing quiz and it is configured to be blocked
	 * @param event Event that has occurred
	 */
	@SuppressWarnings("static-access")
	@EventHandler(priority = EventPriority.HIGH)
	public void onPlayerPickupItem(PlayerPickupItemEvent event) {
		Player player = event.getPlayer();
		
		tagChat = plugin.tagChat;
		conf = plugin.getConf();
		lang = plugin.getLangHandler();
		
		boolean config = conf.getBoolean("Block.Picking-Up", true);
		
		if(plugin.checkBlock(player) && config) {
			event.setCancelled(true);
			player.sendMessage(tagChat + lang.getMessage("NotifyQuizFirst"));
		}
	}
	
	/**
	 * Triggered when player drops an item, block when player is still doing quiz and it is configured to be blocked
	 * @param event Event that has occurred
	 */
	@SuppressWarnings("static-access")
	@EventHandler(priority = EventPriority.HIGH)
	public void onPlayerDropItem(PlayerDropItemEvent event) {
		Player player = event.getPlayer();
		
		tagChat = plugin.tagChat;
		conf = plugin.getConf();
		lang = plugin.getLangHandler();
		
		boolean config = conf.getBoolean("Block.Dropping", true);
		
		if(plugin.checkBlock(player) && config) {
			event.setCancelled(true);
			player.sendMessage(tagChat + lang.getMessage("NotifyQuizFirst"));
		}
	}
	
	/* No needed anymore as onPlayerTeleport also fires on join
	 * 
	 * Triggered when player joins, check if player still has to do a quiz.
	 * @param event Event that has occurred
	 *
	@EventHandler(priority = EventPriority.NORMAL)
	public void onPlayerJoin(PlayerJoinEvent event) {
		//Get the player
		Player player = event.getPlayer();
		
		plugin.checkPlayer(player);
	}*/
	
	/**
	 * Triggered when player joins, check if player still has to do a quiz.
	 * @param event Event that has occurred
	 */
	@EventHandler(priority = EventPriority.NORMAL)
	public void onPlayerTeleport(PlayerTeleportEvent event) {
		//Get the player
		Player player = event.getPlayer();
		event.getFrom().getWorld();
		plugin.checkPlayer(player);
	}
}
